package com.jxsd.edu.cn.tools;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * string  字符处理工具
 * */
public class StringTool 
{
	private  static  Log  logger=LogFactory.getLog("Otherlog");
	
	//去除空格
    public  static String  trimer(String  str)
    {
      	String  s=null;
      	if(str==null||str=="")
      	{
      		s="";
      	}
      	else
      		s=str.trim();
      	return s;
    }
    
    public static String change(String str){
		str=str.replace("<","&lt;");
		str=str.replace(">","&gt;");
		return str;		
	}
	
    //字符串改为中文编码
	public static String toChinese(String str){
		if(str==null)	str="";
		try {
			//通过String类的构造方法将，指定的字符串转换为“gb2312”编码
			str=new String(str.getBytes("ISO-8859-1"),"gb2312");	
		} catch (UnsupportedEncodingException e) {
			str="";
			logger.debug("StringTool.toChinese(String str).UnsupportedEncodingException");
		}
		return str;
	}
	//将String型数据转换为int型数据的方法
	public static int strToint(String str){			
		if(str==null||str.equals(""))
			str="0";
		int i=0;
		try{
			i=Integer.parseInt(str);
		}catch(NumberFormatException e){
			i=0;
			logger.debug("StringTool.strToint(String str).NumberFormatException");
		}
		return i;		
	}
	
	//获取随机验证码
	public static  String   getAuthCode()
	{
		String  authCode=null;
		Random  rand=new  Random();
		char s[]={'A','B','C','D','E','F','G','H','I','J','K',
				  'L','M','N','O','P','Q','R','S','T','U','V',
				  'W','X','Y','Z','0','1','2','3','4','5','6',
				  '7','8','9'};
		StringBuilder  str=new  StringBuilder();
		for(int i=0;i<20;i++)
		{
			int  index=rand.nextInt(35);
			str.append(s[index]);
		}
		authCode=str.toString();
		return  authCode;
	}
	//编号生成器
	public  static  String  getUserId()
	{
		String  userId=null;
		StringBuilder  str=new  StringBuilder();
		Date date=new Date();
	    str.append(date.getTime());
		userId=str.toString();
		return userId;
	}
	//获取用户邮箱登陆网址
	public  static String  getEmailUrl(String mail)
	{
		String[] post_mail =mail.split("@");
		String url_mail=post_mail[1];
		String  forward=null;
		if(url_mail.equals("qq.com"))
		{
			forward=("http://mail.qq.com");
		}
		else {
			if(url_mail.equals("gmail.com"))
			{
				forward=("http://mail.google.com");
			}
			else {
				if(url_mail.equals("sina.com"))
				{
					forward=("http://mail.sina.com.cn");
				}
				else {
					if(url_mail.equals("163.com"))
					{
						forward=("http://mail.163.com");
					}
					else {
						if(url_mail.equals("sohu.com"))
						{
							forward=("http://mail.sogou.com/");
						}
						else {
							if(url_mail.equals("139.com"))
							{
								forward=("http://mail.10086.cn/");
							}
							else {
								if(url_mail.equals("hotmail.com"))
								{
									forward=("http://www.hotmail.com");
								}
								else {
									
									forward=("/jxsdsecondhand/jsp/Register.jsp");
								}
							}
						}
					}
				}
			}
		}
		return forward;
	}
	
	//获取当前时间
	public  static  String  getCurDate()
	{
		 Date date=new Date();
		 String   s;
		 SimpleDateFormat  simple=new  SimpleDateFormat("yyyy-MM-dd");
		 s=simple.format(date);
		 return  s;
	}
	//获取物品信息发布的编号
	public  static  String  getPublishId(String  smallId)
	{
	   String  publishId=null;
	    StringBuilder  str=new StringBuilder();
	    str.append(smallId);
	    Date date=new Date();
	    str.append(date.getTime());
		publishId=str.toString();  
	   return  publishId;
	}
	//不区分字符大小写的替换
	public static String replaceString(String source,String oldstring,String newstring){ 

        System.out.println("原来的字符串："+source); 

        String result1 = source.replaceAll("(?i)"+oldstring, newstring); //大小写不敏感 

        System.out.println("不区分大小写的替换结果："+result1); 
        return  result1;
    } 
	
	public  static  void main(String[] args)
	{
		replaceString("Core  Java","c","<font color='#FF0000' >c</font>");
	}
	
}
